package com.liu.SRMan.web;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.g4studio.core.json.JsonHelper;
import org.g4studio.core.metatype.Dto;
import org.g4studio.core.metatype.impl.BaseDto;
import org.g4studio.core.mvc.xstruts.action.ActionForm;
import org.g4studio.core.mvc.xstruts.action.ActionForward;
import org.g4studio.core.mvc.xstruts.action.ActionMapping;
import org.g4studio.core.mvc.xstruts.upload.FormFile;
import org.g4studio.core.util.G4Constants;
import org.g4studio.core.web.BizAction;
import org.g4studio.core.web.CommonActionForm;
import com.liu.SRMan.service.HorizontalService;


public class HorizontalAction extends BizAction {

	private HorizontalService horizontalService = (HorizontalService) getService("horizontalService");

	/*
	 * 进入新建横向项目页面用action
	 */
	public ActionForward newHorizontalProject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("newHorizontalProject");
	}

	/*
	 * 进入横向项目一览页面用action
	 */
	public ActionForward overView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		return mapping.findForward("overView");
	}

	/*
	 * 进入横向项目结项页面用action
	 */
	public ActionForward finishView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		return mapping.findForward("finishView");
	}

	/*
	 * 保存横向项目用action
	 */
	public ActionForward saveHorizontalProject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);

		FormFile myFile = aForm.getFile1();
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\horizontal\\";
		String user_id = getSessionContainer(request).getUserInfo().getUserid();
		
		Dto outDto = horizontalService.newHorizontal(inDto, myFile, user_id, savePath);
		
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}

	/*
	 * 横向项目检索用action
	 */
	public ActionForward queryHorizontalInfo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		List list = g4Reader.queryForPage("Temp.queryHorizontal", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countHorizontal", dto);
		System.out.println("接收到的表单提交参数：\n" + dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	public ActionForward queryMembers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		System.out.println("接收到的表单提交参数：\n" + dto);

		List list = g4Reader.queryForList("Temp.queryProjectMembers", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countProjectMembers", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				null);

		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	public ActionForward queryFinishHorizontal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		System.out.println("接收到的表单提交参数：\n" + dto);

		List list = g4Reader.queryForPage("Temp.queryFinish", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countFinish", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				null);

		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	public ActionForward finishHorizontal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		System.out.println("接收到的表单提交参数：\n" + dto);

		horizontalService.finishHorizontal(dto);

		setOkTipMsg("项目结项成功", response);
		return mapping.findForward(null);
	}

	/**
	 * 更新横向项目
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateHorizontal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm cForm = (CommonActionForm) form;
		Dto inDto = cForm.getParamAsDto(request);

		FormFile myFile = cForm.getFile1();
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\horizontal\\";
		String user_id = getSessionContainer(request).getUserInfo().getUserid();
		
		Dto outDto = horizontalService.updateHorizontal(inDto,myFile,user_id,savePath);
		
		write(JsonHelper.encodeObject2Json(outDto), response);
		System.out.println("接收到的表单提交参数：\n" + inDto);
		setOkTipMsg("数据修改成功", response);
		return mapping.findForward(null);
	}

	/**
	 * 删除横向项目
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteHorizontal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		
		Dto inDto = aForm.getParamAsDto(request);
		Dto temp = (BaseDto)g4Reader.queryForObject("Temp.getHProjectById", inDto);
		
		System.out.println("取得的纵向项目数据:：\n" + temp);
		String filePath = temp.getAsString("attachment");
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\horizontal\\";
		
		Dto outDto = horizontalService.deleteHorizontal(inDto,filePath,savePath);
		
		write(JsonHelper.encodeObject2Json(outDto), response);
		setOkTipMsg("横向项目删除成功", response);
		return mapping.findForward(null);
	}

}